const { default: Toast } = require("../../../../component/vant/toast/toast.js");
// pages/couponlist/couponlist.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"),
common_Js = require("../../../../component/common.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    state:0,
    pageSize:10,
    pageIndex:1,
    storeQrList:[],
    hasData:false,
    tabIndex:0,
    netWork:false,
    isLoading:true,
    states: [0, 1, 2],
    showDialog:false,
    showQrUrl:'',
    store_id:0,
    storeQr:{},
    couponStore:{},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.data.store_id = options.id
    }
    this.onHttpInit();
  },
  handleChange({ detail}) {
    console.log('handleChange',detail)
    var that = this;
    this.data.pageIndex = 1;
    this.data.current = detail.index
    this.data.state = this.data.states[detail.index]
    that.setData({
      current: detail.index,
      storeQrList:[],
      isLoading:true,
      tabIndex: that.data.state 
    });
    that.onHttpInit()
  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/bingge/store/qr/list', {
      store_id: that.data.store_id,
      state: that.data.state,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      console.log("二维码列表数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var storeQrList = res.data.data.storeQrList;
          that.setData({
            couponStore: res.data.data.couponStore
          })
          if(storeQrList.length > 0){
            for (var index in storeQrList) {
              that.data.storeQrList.splice(that.data.storeQrList.length, 0, storeQrList[index]);
            }
            that.setData({
              hasData: true,
              storeQrList: that.data.storeQrList
            })
          }
        }
      }
      that.setData({ netWork: true, isLoading: false, })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  showQr(e){
    console.log('showQr', e)
    var item = e.currentTarget.dataset.item;
    if(item.state == 0){
      this.setData({
        showQrUrl: item.qrCode,
        storeQr: item,
        showDialog: true,
      })
    }
  },
  onClose(){
    this.setData({
      showDialog: false
    })
  },
  downloadQrCode() {
    var that = this;
    wx.downloadFile({
      url: that.data.showQrUrl,
      success:function(res){
        var filePath = res.tempFilePath
        wx.saveImageToPhotosAlbum({  //保存图片到相册
          filePath: filePath,
          success: function (res) {
            wx.showToast({
              title: "保存图片成功！",
              duration: 2000
            })
            that.setData({
              flag:false
            })
          },fail(res){
            console.log('保存图片 失败', res)
          }
        })
      }
    })
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      storeQrList:[],
      isLoading:true,
    });
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onHttpInit()
  },
})